/********************************************************************
	author:		Jim Pelton and Vanessa Gertman
	purpose:	Parse the configuration file for the Improved Cuda Volume Renderer

    CS464 Fall 2011 Final Project
    Prof. Alark Joshi
    Boise State University
*********************************************************************/


#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <strstream>
#include <vector>
#include <map>
#include <vector_types.h>

struct IsoPair{
    int isoval;
    float isoalpha;
};


using namespace std;

pair<string,float> makepair(string s, string f)
{
    float n = atof(f.c_str());
    return pair<string,float>(s,n);
}

int parseTrans(char *fname, map<string, float> *options, vector<float4> *myTfunc, vector<IsoPair> *myIsoVals) {

    ifstream file;
    file.open(fname);
    string line;
    int tlinesFound = 0;
    int entries=0;
    float n = 0.f;

    if (!file.is_open()) 
    {
        cout << "**Error: Transfer function file couldn't be opened." << endl;
        return 0;
    }

    getline(file, line);
    while (!file.eof()){

        ////////////////////////////isovalues////////////////////////////////
        if (line.compare("iv:") == 0){
            int isoidx = 0;
            //getline(file,line);
            while (!file.eof() && isoidx < 3)
                //while (!file.eof() && strcmp(line.c_str(), "den:") != 0)
            {
                getline(file, line);               
                IsoPair ip;
                strstream ss; ss<<line;
                ss >> ip.isoval;
                ss >> ip.isoalpha;
                myIsoVals->push_back(ip);
                isoidx++;
            }
        }else

        

        if (strcmp(line.c_str(), "den:") == 0)
        {
            getline(file,line);
            options->insert(makepair("den",line));
        }else

        if (strcmp(line.c_str(), "brt:") == 0)
        {
            getline(file,line);
            options->insert(makepair("brt",line));
        }else

        if (strcmp(line.c_str(), "tranoff:") == 0) 
        {
            getline(file,line);
            options->insert(makepair("tranoff",line));
        }else

        if (strcmp(line.c_str(), "transcl:") == 0)
        {
            getline(file,line);
            options->insert(makepair("transcl",line));

        }else

        ///////////////////////////iso value falloff//////////////////////////////
        if (strcmp(line.c_str(), "falloff:") == 0)
        {
            getline(file,line);
            options->insert(makepair("fo",line));

        }else


        //////////////////////start up method////////////////////////////////////

        if (strcmp(line.c_str(), "m:") == 0)
        {
            getline(file,line);
            options->insert(makepair("m",line));

        }else

        //////////////////////////lighting settings/////////////////////////////
        if (strcmp(line.c_str(), "ltpos:") == 0)
        {
            getline(file,line);
            options->insert(makepair("ltx",line));

            getline(file,line);
            options->insert(makepair("lty",line));

            getline(file,line);
            options->insert(makepair("ltz",line));
        }else

        if (strcmp(line.c_str(), "ltmat:") == 0)
        {
            getline(file,line);
            options->insert(makepair("mat_a",line));

            getline(file,line);
            options->insert(makepair("mat_d",line));

            getline(file,line);
            options->insert(makepair("mat_s",line));
        }else

        ///////////////////////////boundary/////////////////////////////////////
        if (strcmp(line.c_str(), "kgc:") == 0)
        {
            getline(file,line);
            float m_g = atof(line.c_str());
            options->insert(makepair("kgc",line));
        }else

        if (strcmp(line.c_str(), "kgs:") == 0)
        {
            getline(file,line);
            float m_g = atof(line.c_str());
            options->insert(makepair("kgs",line));
        }else

        if (strcmp(line.c_str(), "kge:") == 0)
        {
            getline(file,line);
            float m_g = atof(line.c_str());
            options->insert(makepair("kge",line));
        }else


        ///////////////////////////sillllhouettes//////////////////////////////////

        if (strcmp(line.c_str(), "ksc:") == 0)
        {
            getline(file,line);
            float m_g = atof(line.c_str());
            options->insert(makepair("ksc",line));
        }else

        if (strcmp(line.c_str(), "kss:") == 0)
        {
            getline(file,line);
            float m_g = atof(line.c_str());
            options->insert(makepair("kss",line));
        }else

        if (strcmp(line.c_str(), "kse:") == 0)
        {
            getline(file,line);
            float m_g = atof(line.c_str());
            options->insert(makepair("kse",line));

        }else

        ///////////////////////depth color stuff//////////////////////////////////
        if (strcmp(line.c_str(), "kds:") == 0)
        {
            getline(file,line);
            float m_g = atof(line.c_str());
            options->insert(makepair("kds",line));

        }else

        if (strcmp(line.c_str(), "kde:") == 0)
        {
            getline(file,line);
            float m_g = atof(line.c_str());
            options->insert(makepair("kde",line));

        }else

        /////////////////////////tone shading/////////////////////////////////////
        if (strcmp(line.c_str(), "ktb:") == 0)
        {
            getline(file,line);
            float m_g = atof(line.c_str());
            options->insert(makepair("ktb",line));

        }else

        if (strcmp(line.c_str(), "ktd:") == 0)
        {
            getline(file,line);
            float m_g = atof(line.c_str());
            options->insert(makepair("ktd",line));

        }else

        if (strcmp(line.c_str(), "kty:") == 0)
        {
            getline(file,line);
            float m_g = atof(line.c_str());
            options->insert(makepair("kty",line));
        }else


        ///////////////////////transfer quads/////////////////////////////////////
        if (line.compare("tf:") == 0){
            file >> entries;
            //cout << "{" << endl;
            while (!file.eof() && tlinesFound < entries*4)
            {
                float4 f4;
                file >> f4.x >> f4.y>> f4.z >> f4.w;
                myTfunc->push_back(f4);
                //         cout << "{ " << f4.x << ", "
                //             << f4.y << ", "
                //             << f4.z << ", "
                //             << f4.w << " "
                //             << " }" << endl;
                tlinesFound +=4;
            }
            //cout << "}" << endl;
        }
        getline(file,line);
    } //while(!file.eof())
    file.close();

    if (tlinesFound/4 != entries){
        cout << "**Error: tlinesFound/4 != entries!\n";
    }

    return tlinesFound/4;
}

